<template>
  <div class="wrap-item">
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.orgName"
          placeholder="请输入机构名称"
          maxlength="30"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-item-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
        row-key="id"
        default-expand-all
        :tree-props="{ children: 'child', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="orgName" label="机构名称"></el-table-column>
        <el-table-column label="机构类型">
          <template slot-scope="scope">
            <span v-if="scope.row.orgType == 0">机构(组织或单位) </span>
            <span v-if="scope.row.orgType == 1">部门(科室或内置机构)</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="详细地址"></el-table-column>
        <el-table-column prop="description" label="描述" width="300px">
          <template slot-scope="scope">
            <el-tooltip
              popper-class="ellipsis-tip"
              :content="scope.row.orgDesc"
              placement="top-start"
            >
              <span class="ellipsis">{{ scope.row.orgDesc }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="90px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item v-if="$checkPermission(101)" command="add"
                  >添加</el-dropdown-item
                >
                <el-dropdown-item
                  v-if="scope.row.parentNo != 0 && $checkPermission(102)"
                  command="edit"
                  >编辑</el-dropdown-item
                >
                <el-dropdown-item
                  v-if="scope.row.parentNo != 0 && $checkPermission(103)"
                  command="remove"
                  >删除</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 对话框 -->
    <el-drawer :title="title" :wrapperClosable="false" :visible.sync="visible">
      <el-form label-width="110px" :model="form" size="medium">
        <el-form-item label="机构类型" required>
          <el-radio-group v-model="form.orgType" size="medium">
            <el-radio label="0" border>机构(组织或单位)</el-radio>
            <el-radio label="1" border>部门(科室或内置机构)</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="机构名称" required>
          <el-input
            v-model.trim="form.orgName"
            maxlength="30"
            placeholder="请输入机构名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="统一信用代码"  v-if="form.orgType == 0">
          <el-input
            v-model.trim="form.orgCard"
            maxlength="18"
            placeholder="请输入统一信用代码"
          ></el-input>
        </el-form-item>
        <el-form-item label="办公地址"  v-if="form.orgType == 0">
          <el-input
            type="textarea"
            v-model.trim="form.address"
            placeholder="请输入办公地址"
            maxlength="50"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item label="证照附件" required v-if="form.orgType == '0'">
          <web-upload @upload="upload" accept="image/*" />
          <img
            v-if="form.files.length > 0"
            :src="form.files[0].fileDomain + form.files[0].url"
            style="
              position: absolute;
              top: 0;
              left: 100px;
              width: 82px;
              max-height: 82px;
            "
          />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            type="textarea"
            v-model.trim="form.orgDesc"
            placeholder="请输入描述"
            maxlength="100"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 查看 -->
    <el-dialog title="查看" :visible.sync="isLook" width="888px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="机构类型">
          {{
            lockData.orgType == 0 ? '机构(组织或单位)' : '部门(科室或内置机构)'
          }}
        </el-descriptions-item>
        <el-descriptions-item label="上级机构">
          {{ lockData.parentName }}
        </el-descriptions-item>
        <el-descriptions-item label="机构名称">
          {{ lockData.orgName }}
        </el-descriptions-item>
        <el-descriptions-item
          v-if="lockData.orgType == '0'"
          label="统一信用代码"
        >
          {{ lockData.orgCard }}
        </el-descriptions-item>
        <el-descriptions-item
          v-if="lockData.orgType == '0'"
          label="办公地址"
          :span="2"
        >
          {{ lockData.address }}
        </el-descriptions-item>
        <el-descriptions-item label="描述" :span="2">
          {{ lockData.orgDesc }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      visible: false,
      title: '添加机构',
      form: {},
      isLook: false,
      lockData: {},
    }
  },
  created() {
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表数据
      this.$axios.post('/admin/org/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          this.$axios
            .get('/admin/org/find?flag=false&id=' + row.id)
            .then((data) => {
              this.lockData = {}
              if (data.code == 200) {
                this.lockData = data.data
                this.isLook = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'add':
          this.title = '添加机构'
          this.form = {
            orgType: '1',
            parentName: row.orgName,
            parentNo: row.id,
            files: [],
          }
          this.visible = true
          break
        case 'edit':
          this.title = '编辑机构'
          this.$axios
            .get('/admin/org/find?flag=true&id=' + row.id)
            .then((data) => {
              this.form = {
                orgType: '1',
                parentName: row.orgName,
                parentNo: row.id,
                files: [],
              }
              if (data.code == 200) {
                this.form = data.data
                this.visible = true
              } else {
                this.$message.error(data.msg)
              }
            })
          break
        case 'remove':
          this.$confirm('您确定要删除吗', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          })
            .then(() => {
              this.$axios.get('/admin/org/del?id=' + row.id).then((data) => {
                if (data.code == 200) {
                  this.itemList()
                  this.$message.success('操作成功！')
                } else {
                  this.$message.error(data.msg)
                }
              })
            })
            .catch(() => {})
          break
      }
    },
    save() {
      //保存
      if (!this.form.orgName) {
        this.$message.error('请输入机构名称')
        return
      }
      if (this.form.orgType == 0 && !this.form.orgCard) {
        this.$message.error('请输入统一信用代码')
        return
      }
      if (this.form.orgType == 0 && !this.form.address) {
        this.$message.error('请输入办公地址')
        return
      }
      this.$axios.post('/admin/org/edit', this.form).then((data) => {
        if (data.code == 200) {
          this.visible = false
          this.$message.success('操作成功！')
          this.itemList()
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    upload(data) {
      //上传
      this.form.files = []
      this.form.files.push(data)
    },
  },
}
</script>
